package com.hotel.action;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hotel.dto.Envelope;
import com.hotel.service.FinanceService;

@Controller
@RequestMapping("/finance")
public class FinanceAction {
	private FinanceService financeService;
	public FinanceAction() {
		financeService=new FinanceService();
	}
	@RequestMapping("/mainMenu")
	public ModelAndView mainMenu(){
		ModelAndView model= new ModelAndView("finance/mainMenu");
		return model;
	}
	@RequestMapping(value="/executeExchange.do",method=RequestMethod.POST)
	@ResponseBody
	public String executeExchange(String name,double amount){
		try{
			return financeService.executeExchange(name, amount).toString();
		}catch (Exception e) {
			e.printStackTrace();
			return Envelope.systemError.toString();
		}
	}
	@RequestMapping(value="/changeTypePrice.do",method=RequestMethod.POST)
	@ResponseBody
	public String changeTypePrice(int id,double price){
		try{
			return financeService.changeTypePrice(id, price).toString();
		}catch (Exception e) {
			e.printStackTrace();
			return Envelope.systemError.toString();
		}
	}
	@RequestMapping(value="/changeTypeDiscount.do",method=RequestMethod.POST)
	@ResponseBody
	public String changeTypeDiscount(int id,int discount){
		try{
			return financeService.changeTypeDiscount(id, discount).toString();
		}catch (Exception e) {
			e.printStackTrace();
			return Envelope.systemError.toString();
		}
	}
	@RequestMapping(value="/changeRoomDiscount.do")
	@ResponseBody
	public String changeRoomDiscount(int id,int discount){
		try{
			return financeService.changeRoomDiscount(id, discount).toString();
		}catch (Exception e) {
			e.printStackTrace();
			return Envelope.systemError.toString();
		}
	}
	@RequestMapping("/exchangePage")
	public ModelAndView exchangePage(HttpSession session,@RequestParam(value="page", required=false, defaultValue="1")int page){
		ModelAndView model= new ModelAndView("finance/exchange");
		Envelope env=financeService.getFinanceList(page);
		if(env.getCode()!=0){
			session.setAttribute("errorMessage",env.getMessage());
			return new ModelAndView("system/error");
		}
		
		model.addObject("records", env.getObj());
		model.addObject("balance",financeService.getCurrentBalance().getObj());
		model.addObject("currentPage", page);
		return model;
	}
	@RequestMapping("/roomPricePage")
	public ModelAndView roomPricePage(HttpSession session,@RequestParam(value="page", required=false, defaultValue="1")int page){
		ModelAndView model= new ModelAndView("finance/roomPrice");
		Envelope env=financeService.getRoomPriceList(page);
		if(env.getCode()!=0){
			session.setAttribute("errorMessage",env.getMessage());
			return new ModelAndView("system/error");
		}
		model.addObject("rooms", env.getObj());
		model.addObject("currentPage", page);
		return model;
	}
	@RequestMapping("/typePricePage")
	public ModelAndView typePricePage(HttpSession session,@RequestParam(value="page", required=false, defaultValue="1")int page){
		ModelAndView model= new ModelAndView("finance/typePrice");
		Envelope env=financeService.getTypePriceList(page);
		if(env.getCode()!=0){
			session.setAttribute("errorMessage",env.getMessage());
			return new ModelAndView("system/error");
		}
		model.addObject("types", env.getObj());
		model.addObject("currentPage", page);
		return model;
	}
	//public Mo
}
